import { Component, PropTypes } from 'labrador-immutable';
const { string, number, array } = PropTypes;

class Resource extends Component {
  static propTypes = {
    video: string,
    image: array,
    desc: string,
    id: number,
  };

  state = {
    videoProcess: 0,
    imageProcess: 0,
  }

  constructor(props) {
    super(props);
  }

  copyText(event) {
    wx.setClipboardData({
      data: this.props.desc,
      success: function(res) {
      }
    })
  }

  downloadVideo() {
    if (this.state.videoProcess > 0) {
      return
    }
    this.checkSetting(() => {
      const downloadTask = wx.downloadFile({
        url: this.props.video,
        success: (res) => {
        },
        fail: function(err) {
        },
        complete: function(res) {
          if (res.statusCode === 200) {
            wx.saveVideoToPhotosAlbum({
              filePath: res.tempFilePath,
              success() {
                wx.showToast({
                  title:'下载完成～',
                  duration:1000
                });
              },
              fail(err) {
                wx.showToast({
                  title:err.errMsg,
                  duration:1000
                });
              }
            })
          } else {
            wx.showToast({
              title:'下载失败！',
              duration:1000
            });
          }
        }
      });
      downloadTask.onProgressUpdate((res) => {
        this.setState({ videoProcess: res.progress })
      })
    })
  }

  downloadImages() {
    wx.navigateTo({ url: '/pages/download/index?images=' + JSON.stringify(this.props.image)});
    return;
    if (this.state.imageProcess > 0) {
      return
    }
    this.setState({ imageProcess: 1 });
    this.checkSetting(() => {
      this.downloadImage(0);
    })
  }

  downloadImage(index) {
    const { image } = this.props;
    if (index >= image.length) {
      return;
    }
    var that = this;
    const currentUrl = image[index];
    const downloadTask = wx.downloadFile({
      url: currentUrl,
      success: (res) => {
      },
      fail: function(err) {
      },
      complete: function(res) {
        if (res.statusCode === 200) {
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success(res) {
              that.downloadImage(index + 1);
              if (index === image.length - 1) {
                wx.showToast({
                  title:'下载完成～',
                  duration:1000
                });
              }
            },
            fail(err) {
              wx.showToast({
                title:err.errMsg,
                duration:1000
              });
            }
          })
        }
      }
    });
    const completeProcess = 100 * index;
    downloadTask.onProgressUpdate((res) => {          
      this.setState({ imageProcess: parseInt((completeProcess + res.progress)/image.length) });
    })
  }

  checkSetting(callback) {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              callback()
            }
          })
        } else {
          callback()
        }
      }
    })
  }

  previewImage(event) {
    const previewItem = event.currentTarget.dataset.previewItem;
    wx.previewImage({
      current: previewItem,
      urls: this.props.image
    })
  }
}

export default Resource;
